package cn.kpt.biz.controller;

import cn.kpt.beans.dto.Dto;
import cn.kpt.beans.vo.ItripImageVO;
import cn.kpt.beans.vo.comment.ItripListCommentVO;
import cn.kpt.beans.vo.comment.ItripScoreCommentVO;
import cn.kpt.beans.vo.comment.ItripSearchCommentVO;
import cn.kpt.biz.service.CommentService;
import cn.kpt.utils.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @ClassName CommentController
 * @Description TODO
 * @Author kangpengtao
 * @Date 2019/10/21 15:29
 */
@RestController
@RequestMapping("/api/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    /**
     * 酒店详情-评论图片
     * @param targetId
     * @return
     */
    @RequestMapping(value = "/getimg/{targetId}",method = RequestMethod.GET)
    public Dto doGetImg(@PathVariable Long targetId){
        try {
            List<ItripImageVO> vos = commentService.getCommentImageById(targetId);
            return DtoUtil.returnDataSuccess(vos);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询图片失败！",ErrorCode.BIZ_UNKNOWN);
        }
    }

    /**
     * 酒店详情-评论列表查询
     * @param vo
     * @return
     */
    @ApiOperation(value = "根据评论类型查询评论列表，并分页显示", httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = Dto.class,notes = "根据评论类型查询评论列表，并分页显示"+
            "<p>参数数据e.g：</p>" +
            "<p>全部评论：{\"hotelId\":10,\"isHavingImg\":-1,\"isOk\":-1,\"pageSize\":5,\"pageNo\":1}</p>" +
            "<p>有图片：{\"hotelId\":10,\"isHavingImg\":1,\"isOk\":-1,\"pageSize\":5,\"pageNo\":1}</p>" +
            "<p>值得推荐：{\"hotelId\":10,\"isHavingImg\":-1,\"isOk\":1,\"pageSize\":5,\"pageNo\":1}</p>" +
            "<p>有待改善：{\"hotelId\":10,\"isHavingImg\":-1,\"isOk\":0,\"pageSize\":5,\"pageNo\":1}</p>" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>"+
            "<p>100020 : 获取评论列表错误 </p>")
    @RequestMapping(value = "/getcommentlist",method = RequestMethod.POST)
    public Dto doGetCommentList(@RequestBody ItripSearchCommentVO vo){
        try {
            if(EmptyUtils.isEmpty(vo.getHotelId())){
                return DtoUtil.returnFail("酒店编号不能为空！", ErrorCode.BIZ_PARAMETER_ERROR);
            }
            Page<ItripListCommentVO> page = new Page<>();
            page.setPageSize(vo.getPageSize() == null ? Constants.DEFAULT_PAGE_SIZE : vo.getPageSize());
            page.setCurPage(vo.getPageNo() == null ? Constants.DEFAULT_PAGE_NO : vo.getPageNo());
            // 封装Page数据
            commentService.getCommentListForPaging(page,vo);
            return DtoUtil.returnDataSuccess(page);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询评论列表失败！", ErrorCode.BIZ_UNKNOWN);
        }
    }
    /**
     * 根据酒店id查询各类评论数量
     * @param hotelId
     * @return
     */
    @ApiOperation(value = "根据酒店id查询各类评论数量", httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = Dto.class,notes = "根据酒店id查询评论数量（全部评论、值得推荐、有待改善、有图片）"+
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>"+
            "<p>100014 : 获取酒店总评论数失败 </p>"+
            "<p>100015 : 获取酒店有图片评论数失败</p>"+
            "<p>100016 : 获取酒店有待改善评论数失败</p>"+
            "<p>100017 : 获取酒店值得推荐评论数失败</p>"+
            "<p>100018 : 参数hotelId为空</p>")
    @RequestMapping(value = "/getcount/{hotelId}",method= RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto getCommentCountByType(@ApiParam(required = true, name = "hotelId", value = "酒店ID")
                                     @PathVariable String hotelId){
        Integer count = 0;
        Map<String,Integer> countMap = new HashMap<String,Integer>();
        Map<String,Object> param = new HashMap<String,Object>();
        if(null != hotelId && !"".equals(hotelId)){
            param.put("hotelId",hotelId);
            count = getItripCommentCountByMap(param);
            if(count != -1){
                countMap.put("allcomment",count);
            }else{
                return DtoUtil.returnFail("获取酒店总评论数失败","100014");
            }
            param.put("isOk",1);//0：有待改善 1：值得推荐
            count = getItripCommentCountByMap(param);
            if(count != -1){
                countMap.put("isok",count);
            }else{
                return DtoUtil.returnFail("获取酒店值得推荐评论数失败","100017");
            }
            param.put("isOk",0);//0：有待改善 1：值得推荐
            count = getItripCommentCountByMap(param);
            if(count != -1){
                countMap.put("improve",count);
            }else{
                return DtoUtil.returnFail("获取酒店有待改善评论数失败","100016");
            }
            param.put("isHavingImg",1);//0:无图片 1:有图片
            param.put("isOk",null);
            count = getItripCommentCountByMap(param);
            if(count != -1){
                countMap.put("havingimg",count);
            }else{
                return DtoUtil.returnFail("获取酒店有图片评论数失败","100015");
            }

        }else{
            return DtoUtil.returnFail("参数hotelId为空","100018");
        }
        return DtoUtil.returnSuccess("获取酒店各类评论数成功",countMap);
    }

    public Integer getItripCommentCountByMap(Map<String,Object> param){
        Integer count  = -1;
        try {
            count =  commentService.getItripCommentCountByMap(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 酒店详情-酒店评分
     * @param hotelId
     * @return
     */
    @ApiOperation(value = "据酒店id查询酒店平均分", httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = Dto.class,notes = "总体评分、位置评分、设施评分、服务评分、卫生评分"+
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>"+
            "<p>100001 : 获取评分失败 </p>"+
            "<p>100002 : hotelId不能为空</p>")
    @RequestMapping(value = "/gethotelscore/{hotelId}")
    public Dto doGetHotelScore(@PathVariable Long hotelId){
        try{
            ItripScoreCommentVO vo = commentService.getCommentScoreByHotelId(hotelId);
            return DtoUtil.returnDataSuccess(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询评分失败！", ErrorCode.BIZ_UNKNOWN);
        }
    }
}
